<?php
namespace app\admin\controller;
use think\Db;
class Customer extends Base{
    public function customer_list(){
        $key=input('get.key');
        if (!empty(input('get.fromtime'))) {
            $fromtime=strtotime(input('get.fromtime'));
        }else{
            $fromtime=0;
        }
        if (!empty(input('get.totime'))) {
            $totime=strtotime(input('get.totime'))+86400;
        }else{
            $totime=time();
        }
        $list=Db::name('customer')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('cname|account','like',"%{$key}%")->order('id desc')->paginate(10);
        $lists=Db::name('customer')->where('time','>',"{$fromtime}")->where('time','<',"{$totime}")->where('cname|account','like',"%{$key}%")->select();
        $data=$list->all();
        
        $this->assign('key',input('get.key'));
        $this->assign('fromtime',input('get.fromtime'));
        $this->assign('totime',input('get.totime'));
        $this->assign('data',$data);
        $this->assign('list',$list);
        $this->assign('lists',$lists);
        return $this->fetch();
    }
    public function customer_add(){
        if(request()->isGet()){
            // $model = [1=>'企业',2=>'培训',3=>'资讯'];
            $model = [1=>'企业'];
            $this->assign('model',$model);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            $data['model']=$param['model'];
            $data['cname']=$param['cname'];
            $data['account']=$param['account'];
            $data['password']=md5('tmd'.$param['password']);
            $data['platformwx_state']=2;
            $data['platformpc_state']=2;
            $data['platformbd_state']=2;
            $data['platformtt_state']=2;
            $data['template']=$param['template'];
            $data['state']=1;
            $data['time']=time();
            $data['uptime']=time();
            $data['ctoken']=md5('tmd'.$param['account'].$data['time']);
            $customer=Db::name('customer')->where('account',$param['account'])->find();
            if($customer){
                return json(['code' => 500,'msg' => '账号同名']);
            }else{
                $res=Db::name('customer')->insertGetId($data);
                if($res){
                    if($data['model']==1){
                        $modules=Db::name('moduleset')->order('id asc')->limit(0,9)->select();
                        for ($i=0; $i <count($modules) ; $i++) { 
                            $modules[$i]['cus_id']=$res;
                            unset($modules[$i]['id']);
                            if($res>1){
                                Db::name('moduleset')->insert($modules[$i]);
                            }
                        }
                    }
                    if($data['model']==2){
                        $modules=Db::name('tmoduleset')->order('id asc')->limit(0,9)->select();
                        for ($i=0; $i <count($modules) ; $i++) { 
                            $modules[$i]['cus_id']=$res;
                            unset($modules[$i]['id']);
                            if($res>1){
                                Db::name('tmoduleset')->insert($modules[$i]);
                            }
                        }
                    }
                    if($data['model']==3){
                        $modules=Db::name('nmoduleset')->order('id asc')->limit(0,9)->select();
                        for ($i=0; $i <count($modules) ; $i++) { 
                            $modules[$i]['cus_id']=$res;
                            unset($modules[$i]['id']);
                            if($res>1){
                                Db::name('nmoduleset')->insert($modules[$i]);
                            }
                        }
                    }
                    return json(['code' => 200,'msg' => '成功']);
                }else{
                    return json(['code' => 500,'msg' => '失败']);
                }
            }
        }
    }
    public function customer_edit(){
        if(request()->isGet()){
            $param = input('get.');
            $user=Db::name('customer')->where('id',$param['id'])->find();
            // $model = [1=>'企业',2=>'培训',3=>'资讯'];
            $model = [1=>'企业'];
            $this->assign('model',$model);
            $this->assign('user',$user);
            return $this->fetch();
        }elseif(request()->isPost()){
            $param = input('post.');
            // $data['cname']=$param['cname'];
            // $data['account']=$param['account'];
            $data['model']=$param['model'];
            if(!empty(input("param.password"))){
                $data['password']=md5('tmd'.$param['password']);
            }
            $data['state']=1;
            $data['template']=$param['template'];
            $data['uptime']=time();
            $res= Db::name('customer')->where(array('id'=>$param['id']))->update($data);
            if($res){
                if($data['model']==1){
                    $ismodule=Db::name('moduleset')->where('cus_id',$param['id'])->find();
                    if($ismodule){
                    }else{
                        $modules=Db::name('moduleset')->order('id asc')->limit(0,9)->select();
                        for ($i=0; $i <count($modules) ; $i++) { 
                            $modules[$i]['cus_id']=$param['id'];
                            unset($modules[$i]['id']);
                            Db::name('moduleset')->insert($modules[$i]);
                        }
                    }
                }
                if($data['model']==2){
                    $istmodule=Db::name('tmoduleset')->where('cus_id',$param['id'])->find();
                    if($istmodule){
                    }else{
                        $modules=Db::name('tmoduleset')->order('id asc')->limit(0,9)->select();
                        for ($i=0; $i <count($modules) ; $i++) { 
                            $modules[$i]['cus_id']=$param['id'];
                            unset($modules[$i]['id']);
                            Db::name('tmoduleset')->insert($modules[$i]);
                        }
                    }
                }
                if($data['model']==3){
                    $isnmodule=Db::name('nmoduleset')->where('cus_id',$param['id'])->find();
                    if($isnmodule){
                    }else{
                        $modules=Db::name('nmoduleset')->order('id asc')->limit(0,9)->select();
                        for ($i=0; $i <count($modules) ; $i++) { 
                            $modules[$i]['cus_id']=$param['id'];
                            unset($modules[$i]['id']);
                            Db::name('nmoduleset')->insert($modules[$i]);
                        }
                    }
                }
                return json(['code' => 200,'msg' => '更新成功']);
            }else{
                return json(['code' => 500,'msg' => '失败']);
            }
        }
    }
    public function state_off(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['state'=>2]);
        if($status){
            return json(['code' => 200,'msg' => '已禁止']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function state_on(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['state'=>1]);
        if($status){
            return json(['code' => 200,'msg' => '已开启']);
        }else{
            return json(['code' => 500,'msg' => '出错']);
        }
    }
    public function platformwx_state(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['platformwx_state'=>input('param.platformwx_state')]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '失败']);
        }
    }
    public function platformpc_state(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['platformpc_state'=>input('param.platformpc_state')]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '失败']);
        }
    }
    public function platformbd_state(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['platformbd_state'=>input('param.platformbd_state')]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '失败']);
        }
    }
    public function platformtt_state(){
        $id=input('param.id');
        $status = Db::name('customer')->where(array('id'=>$id))->update(['platformtt_state'=>input('param.platformtt_state')]);
        if($status){
            return json(['code' => 200,'msg' => '成功']);
        }else{
            return json(['code' => 500,'msg' => '失败']);
        }
    }
    public function customer_del(){
        $param = input('post.');
        $res= Db::name('customer')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
    public function customer_dels(){
        $param = input('post.');
        $res= Db::name('customer')->where(array('id'=>$param['id']))->update(['state'=>3]);
        return json(['code' => 200,'msg' => '删除成功']);

    }
}
